# python编程：入门到实践
"11.1 测试函数"

import unittest
from name_function import  get_formatted_name

class NamesTestCase(unittest.TestCase):   # 创建测试用例对象，类名随意
    """ 测试name_function """             

    def test_first_last_name(self):       # 定义一个测试用例,命名要以test开头
        "测试是否正确的识别姓名"
        formatted_name = get_formatted_name("janis", "joplin")
        self.assertEqual(formatted_name, "Janis Joplin")   # 断言方法，两个参数要相等

# unittest.main()

# unittest.TestCase 常用断言方法
# assertEqual(a,b)        -->  a = b
# assertNotEqual(a, b)    -->  a != b
# assertTrue(x)           -->  x is True
# assertFalse(x)          -->  x is False
# assertIn(item, list)    -->  item in list
# assertNotIn(item, list) -->  item not in list


# 匿名调查问卷
class AnonymousSurvey(object):
    '匿名调查问卷'

    def __init__(self, question):  # 调查问卷的问题 和 答案
        '定义一个问题， 并为存储答案做准备'
        self.question = question
        self.responses = []
    
    def show_question(self):
        '显示问卷调查的问题'
        print(question)
    
    def store_response(self, new_response):
        '存储单份调查答案'
        self.responses.append(new_response)

    def show_results(self):
        '显示收集到得所有答案'
        print("Survey result is：")
        for response in self.responses:
            print('-' + response)

# 定义一个问题，并创建一个调查问卷对象
question = "What language did you first learn to speak"
my_survey = AnonymousSurvey(question)

# 显示问题并存储答案
my_survey.show_question()
print("Enter 'q' at any time to quit.\n")
while True:
    response = input("Language: ")
    if response == 'q':
        break
    my_survey.store_response(response)

# 显示调查结果
print("\nThank you to everyone who participated in survey!")
my_survey.show_results()


# /* 对于类的单元测试，调用类中的方法，将结果与预期的结果进行对比 */
# unittest setUp()方法： 创建实例及设置其属性，以方便后面test_* 函数测试